package com.zhong.reflect;

import com.zhong.annotation.ParamValidator;
import com.zhong.model.User;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 *  反射--注解
 * @author:yanglin on 2018/6/17.
 */
public class AnnotationDemo {

    @Test //类获取注解
    public void demo2(){
        Class<User> clazz = User.class;
        RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
//        String[] value = annotation.value();
//        System.err.println(value[0]);
        String name = annotation.name();
        System.err.println(name);
    }

    @Test  //字段获取注解
    public void demo3() throws NoSuchFieldException {
        Class<User> clazz = User.class;
        Field name = clazz.getDeclaredField("name");
        ParamValidator annotation = name.getAnnotation(ParamValidator.class);
        String value = annotation.value();
        boolean required = annotation.required();
        System.err.println(value + "===" + required);
    }
}
